#include "PCH.h"
#include "VertexBufferDX9.h"
#include "enum_convert_dx9.h"
Graphic::VertexBufferDX9::VertexBufferDX9(D3D9VertexBuffer* pVB, int size, const char* data)
	: m_pBuffer(pVB)
	, m_nSize(size)
	, m_bIsValid(true)
{
	if(data)
	{
		Upload(data, size);
	}
}

int Graphic::VertexBufferDX9::GetHandle()
{
	return (int)m_pBuffer;
}

bool Graphic::VertexBufferDX9::IsValid()
{
	return m_bIsValid;
}

void Graphic::VertexBufferDX9::Release()
{
	if(m_bIsValid)
	{
		m_bIsValid = false;
		m_pBuffer->Release();
	}

	delete this;
}

void Graphic::VertexBufferDX9::Upload( const char* data, int size)
{
	char* pBuffer = (char*)Lock(Graphic::BU_WRITE_ONLY);
	memcpy(pBuffer, data, size);
	Unlock();
}

void* Graphic::VertexBufferDX9::Lock( int usage )
{
	void* pBuffer = 0;
	m_bIsValid = false;
	if(FAILED(m_pBuffer->Lock(0, m_nSize, &pBuffer, BU_To_BufferLock_DX9(usage))))
	{
		m_bIsValid = true;
		return 0;
	}
	return pBuffer;
}

void Graphic::VertexBufferDX9::Unlock()
{
	m_pBuffer->Unlock();
	m_bIsValid = true;
}

int Graphic::VertexBufferDX9::GetSize()
{
	return m_nSize;
}
